package edu.ncsu.csc.ip.sender;

import java.util.Timer;
import java.util.TimerTask;

public class TimeManager {

	Timer timer;
	StopNWaitProtocol protocol;

	public TimeManager(StopNWaitProtocol protocol) {
		this.protocol = protocol;
	}

	public void start() {
		timer = new Timer();
		timer.schedule(new TimeoutTask(), 10 * 1000);
	}

	public void stop() {

		timer.cancel();
		;

	}

	class TimeoutTask extends TimerTask {
		public void run() {
			System.out.format("Time's up!%n");
			timer.cancel(); // Terminate the timer thread

			protocol.timoutOccured();
		}
	}

}
